package com.sig.agent;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.lang.instrument.ClassDefinition;
import java.lang.instrument.Instrumentation;

/**
 * 动态代理主入口
 *
 * @author sig
 * @since 2019/3/6
 */
public class SigAgent {

    public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
        File classFile = new File(agentArgs);
        byte[] classFileBytes = new byte[(int) classFile.length()];
        DataInputStream inputStream = new DataInputStream(new FileInputStream(classFile));
        inputStream.readFully(classFileBytes);
        inputStream.close();
        Class<?> updateClass = new SigClassLoader().findClass(classFileBytes);
        instrumentation.redefineClasses(new ClassDefinition(Class.forName(updateClass.getName()), classFileBytes));
    }
}
